var  client = require('../database');
function  Blog(blog) {
    this.title =blog.title;
    this.author =blog.author;
    this.type= blog.type;
    this.tag= blog.tag;
    this.content= blog.content;
}
var tableName = "blog";
mysql = client.getDbCon();
module.exports = Blog;

/** add blog */
Blog.prototype.save = function save(callback) {
    var  blog = {
        title: this.title,
        author: this.author,
        type: this.type,
        tag: this.tag,
        content:this.content
    };
    console.log(blog.title);
    var sql = "insert into blog (title,author,type,tag,content,createTime) values(?,?,?,?,?,now())";
    mysql.query(sql,[blog.title,blog.author,blog.type,blog.tag,blog.content],function(err,results,fields){
        if (err) {
            throw err;
        } else {
            return callback(err, results[0], fields);
        }
    });
};

/** get blogs*/
Blog.get = function get(page,size,callback){
    var sql = "select * from blog order by createTime desc limit ?,?";
    mysql.query(sql,[page,size],function(err,results,fields){
        if (err) {
            throw err;
        } else {
            return callback(err, results, fields);
        }
    });
}

/** get blogs*/
Blog.getById = function getById(id,callback){
    var sql = "select * from blog where id = ?";
    mysql.query(sql,[id],function(err,result,fields){
        if (err) {
            throw err;
        } else {
            return callback(err, result, fields);
        }
    });
}